class Solution {
    public static int next(int n) {
        int sum = 0;
        while(n != 0) {
            int t = n % 10;
            sum += t * t;
            n = n / 10;
        }
        return sum;
    }

    public static boolean isHappy(int n) {
        int fast = next(n),slow = n;
        while(slow != fast) {
            fast = next(next(fast));
            slow = next(slow);
        }
        return slow == 1;
    }

    public static void main(String[] args) {
        System.out.println(isHappy(19));
    }
}